﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace DockerPlay
{
    public class DockerOrchestrator
    {
        private readonly DockerHelper dockerHelper;
        //Start ga_backup_from_a
        //Start ga_mongo_b_27018
        //2.1 wait for the container to exit
        //Run Sync logic // code c#
        //Stop ga_mongo_b_27018
        //Stop ga_mongo_a_27017
        //Start ga_mongo_b_27017

        public DockerOrchestrator(DockerHelper dockerHelper)
        {
            this.dockerHelper = dockerHelper;
        }

        /// <summary>
        /// Start coresponding container in
        /// </summary>
        /// <param name="activeContainer"></param>
        /// <returns></returns>
        public async Task StartSyncContainer(ContainerName activeContainer)
        {
            switch (activeContainer)
            {
                case ContainerName.ga_mongo_a_27017:
                    await CreateBackup(ContainerName.ga_backup_from_a);
                    await dockerHelper.StartContainer(ContainerName.ga_mongo_b_27018);
                    break;

                case ContainerName.ga_mongo_b_27017:
                    await CreateBackup(ContainerName.ga_backup_from_b);
                    await dockerHelper.StartContainer(ContainerName.ga_mongo_a_27018);
                    break;
            }
        }

        public async Task<ContainerName> SwitchActiveContainer(ContainerName activeContainer)
        {
            switch (activeContainer)
            {
                case ContainerName.ga_mongo_a_27017:
                    await SwitchActiveContainerToB();
                    return ContainerName.ga_mongo_b_27017;

                case ContainerName.ga_mongo_b_27017:
                    await SwitchActiveContainerToA();
                    return ContainerName.ga_mongo_a_27017;
            }
            throw new Exception("Active container name do not exists");
        }

        private async Task CreateBackup(ContainerName containerName)
        {
            await dockerHelper.StartContainer(containerName);
            var status = DockerContainerState.running;
            //todo: change while
            while (status != DockerContainerState.exited)
            {
                Thread.Sleep(50);
                status = await dockerHelper.GetContainerStatus(containerName);
            }
        }

        private async Task SwitchActiveContainerToA()
        {
            await dockerHelper.StopContainer(ContainerName.ga_mongo_a_27018);
            await dockerHelper.StopContainer(ContainerName.ga_mongo_b_27017);
            await dockerHelper.StartContainer(ContainerName.ga_mongo_a_27017);
        }

        private async Task SwitchActiveContainerToB()
        {
            await dockerHelper.StopContainer(ContainerName.ga_mongo_b_27018);
            await dockerHelper.StopContainer(ContainerName.ga_mongo_a_27017);
            await dockerHelper.StartContainer(ContainerName.ga_mongo_b_27017);
        }
    }
}
